<?php 
        get_header();
        //if(is_user_logged_in()):
?>
<?php  
        global $wpdb;
        $with = $_GET['with'];
        $query = "SELECT ID,post_title FROM {$wpdb->posts} WHERE post_type='store' AND post_status = 'publish'";
        $exclude = array('OTHER','');
        if(!in_array($with, $exclude))
            $query .=" AND post_title LIKE '$with%'";
        else if($with == 'OTHER')
            $query .= " AND post_title NOT RLIKE '^[A-Z]'";
        else if(!$with)
            $query .= " AND post_title LIKE 'A%'";
        if($with)
            $store_filter = $wpdb->get_results($query);
        $az = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','other');
?>

<div class="row-fluid">
    <div class="span12">
<div class="row-fluid alpha_list">
    <div class="span12 all_store">
        <h2>
        <?php
        $site_url = site_url();  
        foreach ($az as $a) {
            $a = strtoupper($a);
            echo "<a href='{$site_url}/store-listing/?with={$a}'>{$a}</a> ";            
        }
        ?>    
        </h2>
        <?php echo (!in_array($with, $exclude) ? "Stores start with letter [$with]" : "$with STORES"); ?>
    </div>
</div>
<div class="row-fluid result_listing">
    <ul class="list_store">
    <?php              
        $add_cp_link = site_url('wp-admin/post-new.php?post_type=coupon');
        $quick_add_cp = site_url('/quick-add');
        $empty_store = 'style="color: #6f6f6f;font-size:15px"';
        if(count($store_filter) > 0):
        foreach ($store_filter as $st){
            //$qr = " 
//            SELECT COUNT(ID) AS 'total' FROM {$wpdb->posts} 
//            WHERE ID IN 
//            (SELECT post_id FROM {$wpdb->postmeta} WHERE (meta_key = 'store_coupon_metadata' AND meta_value = {$st->ID}))
//            " ;
//            $rs = $wpdb->get_results($qr);            
            ?>                
                <li>
                    <a title="View <?php echo $st->post_title; ?> store" href="<?php echo get_permalink($st->ID); ?>" style="font-size: 15px;"><?php echo $st->post_title; ?></a>
                    
                    <!-- print link 'add coupon' if current user can add new post -->                    
                    <?php if(user_can_create_post(get_current_user_id())) echo "<a rel='nofollow' style='font-size:16px;font-weight:bolder;color:red' title='Add new coupon for {$st->post_title}' target='blank' href='{$add_cp_link}&store_id={$st->ID}'>+</a>"; ?>
                    <?php if(user_can_create_post(get_current_user_id())) echo "<a rel='nofollow' style='font-size:16px;font-weight:bolder;color:blue' title='Quick add coupon for {$st->post_title}' target='blank' href='{$quick_add_cp}?st={$st->ID}'>+</a>"; ?>
                </li>                
            <?php }
            endif;
    ?>
    </ul>        
</div>
</div>  
</div>

<?php  
//endif;
get_footer(); ?>
